package logic

import (
	"api/model/discount"
	"api/service"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelBusDiscountLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDelBusDiscountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelBusDiscountLogic {
	return &DelBusDiscountLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 【商家】删除折扣卡
func (l *DelBusDiscountLogic) DelBusDiscount(req *types.RequestDelDiscount) (resp *types.ResponseMsg, err error) {
	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	businessService := service.NewBusinessService(l.ctx, l.svcCtx)
	business := businessService.GetLoginBusinessInfo()
	discountModel := discount.NewDiscountModel(conn)

	err = discountModel.Delete(l.ctx, req.Id, business.Id)

	redis := l.svcCtx.RedisPool.Get()
	defer redis.Close()

	// 删除redis 销量
	businessService.DelDiscountSale(redis, business.Id)

	if err != nil {
		return service.ReturnError()
	}

	return service.ReturnOk()
}
